package main

import (
	"common"
	"common/log"
	"common/process"
	"errors"
	"euplink"
	"fmt"
	"github.com/spf13/cobra"
	"io/fs"
	"path/filepath"
)

var uploadDirFilesCmd = &cobra.Command{
	Use:   "upload_dir_files",
	Short: "上传目录下的所有文件",
	Long:  "euplink upload_dir_files  dir",
	RunE:  cmdUploadDirFiles,
}

func cmdUploadDirFiles(cmd *cobra.Command, args []string) error {
	ctx, _ := process.Ctx(cmd)
	if len(args) != 1 {
		return errors.New("参数错误")
	}

	zapLog := log.NewZapLog(&runConfig.Log)
	prj, err := euplink.NewProject(ctx, runConfig.Project, zapLog)
	if err != nil {
		return fmt.Errorf("project init err: %s", err)
	}
	prj.StartUpdateNodeServer(ctx)
	rDir := args[0]
	filepath.WalkDir(rDir, func(path string, d fs.DirEntry, err error) error {
		if d.IsDir() {
			return nil
		}
		fp, size, err := common.OpenFile(path)
		if err != nil {
			return nil
		}
		prj.UploadObject(ctx, runConfig.Project.DefaultBucket, path[len(rDir):], fp, false, size, nil)
		return nil
	})

	return nil
}
